﻿using System;
using System.Collections;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormServiceRefreshWorker
	{

		public static string refresh(Form form)
		{
			//CID 0291 //2009.06.01 //ConfigUtil.UseAJAX()
			if( ConfigUtil.UseAJAX() )
			{
				return responseAJAX( form );
			}
			else
			{
				return responseFrame( form );
			}
		}

		public static string responseAJAX(Form form)
		{	
			string ajaxBracket = FormUtil.getAjaxBracket( form );
			if( form.RunClientCodeOnly )
			{
				StringBuilder scriptBuffer = new StringBuilder();
				scriptBuffer.Append( ajaxBracket );
				//如果有ClientCode,输出.
				if( form.ClientCodeList != null )
				{
					foreach( string clientCode in form.ClientCodeList )
					{
						scriptBuffer.Append( clientCode );
					}
				}
				//尽管是RunClientCodeOnly，仍需要重置InfoJet_Postbacking和隐藏Progress。
				scriptBuffer.Append( "InfoJet_StopProgress();" );
				scriptBuffer.Append( "InfoJet_Postbacking = 0;" );
				//CID 0267 //2009.04.07 //Begin
				scriptBuffer.Append( "InfoJet_OnPostbackEnd();" );
				//CID 0267 //2009.04.07 //End
				scriptBuffer.Append( ajaxBracket );
				return scriptBuffer.ToString();
			}

			NoRefreshViewNode noRefreshViewNode = null;
			if( form.ValueChangeNode != null )
			{
				noRefreshViewNode = form.Manifest.FormConfiguration.findNoRefreshViewNode( form.ValueChangeNode );
			}

			XmlElement viewElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.ViewId + "']" );
			XmlElement formElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.FormId + "']" );
			XmlElement dataElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.DataId + "']" );
			//找到Script
			XmlElement scriptElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/script[@id='" + JetConstant.ScriptId + "']" );

			StringBuilder htmlBuffer = new StringBuilder();
			htmlBuffer.Append( ajaxBracket );

			//CID 0135 //2008.10.05 //Comment
			/* 
			if( noRefreshViewNode != null )
			{
				htmlBuffer.Append( "var viewHtml = '';" );
			}
			else
			{
				htmlBuffer.Append( "var viewHtml = '" + ScriptHtmlUtil.formatHtml( viewElement.InnerXml ) + "';" );
			}
			htmlBuffer.Append( "var formHtml = '" + ScriptHtmlUtil.formatHtml( formElement.InnerXml ) + "';" );
			htmlBuffer.Append( "var dataHtml = '" + ScriptHtmlUtil.formatHtml( dataElement.InnerXml ) + "';" );
			*/
			//CID 0135 //2008.10.05 //Begin
			string viewHtml = "";
			if( noRefreshViewNode != null )
			{
				viewHtml = "";
			}
			else
			{
				viewHtml = viewElement.InnerXml;
			}
			string formHtml = formElement.InnerXml;
			string dataHtml = dataElement.InnerXml;
			
			string separator = "----$InfoJet_Separator$----";
			
			htmlBuffer.Append( viewHtml );
			htmlBuffer.Append( separator );
			htmlBuffer.Append( formHtml );
			htmlBuffer.Append( separator );
			htmlBuffer.Append( dataHtml );
			htmlBuffer.Append( separator );
			//CID 0135 //2008.10.05 //End

			//CID 0296 //2009.06.23 //Begin
			if( form.NewStructureNode != null )
			{
				string newStrctureNodeId = DOMUtil.getNodeJetId( form.NewStructureNode );
				htmlBuffer.Append( "InfoJet_NewStructureNodeId = '" + newStrctureNodeId + "';" );
			}
			//CID 0296 //2009.06.23 //End

			//重置InfoJet_Postbacking和隐藏Progress。
			htmlBuffer.Append( "InfoJet_StopProgress();" );
			htmlBuffer.Append( "InfoJet_Postbacking = 0;" );
			//CID 0087 //2008.08.05 //Begin
			htmlBuffer.Append( "InfoJet_OnPostbackEnd();" );
			//CID 0087 //2008.08.05 //End

			//CID 0255 //2009.05.21 //Begin
			//CID 0400 //2009.12.02 //Begin
			if( form.IsOnOpenView )
			{
				htmlBuffer.Append( "InfoJet_FocusControlId = null;" );
			}
			else
			{
                //CID 0828 //2011.03.10 //Begin
                if (form.FocusItemIndex != -1)
                {
                    htmlBuffer.Append("InfoJet_FocusItemIndex = " + form.FocusItemIndex + ";");
                }
                //CID 0828 //2011.03.10 //End
				htmlBuffer.Append( "InfoJet_RestoreFocus();" );
			}
			//CID 0400 //2009.12.02 //End
			//CID 0255 //2009.05.21 //End
			
			//将Script直接输出到AJAX ResponseText中执行.
			htmlBuffer.Append( scriptElement.InnerText );

			//CID 0474 //2009.12.23 //Begin
			htmlBuffer.Append( "InfoJet_ClearCachedPostback();" );
			//CID 0474 //2009.12.23 //End

			htmlBuffer.Append( ajaxBracket );
			return htmlBuffer.ToString();
		}
		
		public static string responseFrame(Form form)
		{
			if( form.RunClientCodeOnly )
			{
				StringBuilder scriptBuffer = new StringBuilder();
				scriptBuffer.Append( "<script>" );
				//如果有ClientCode,输出.
				if( form.ClientCodeList != null )
				{
					foreach( string clientCode in form.ClientCodeList )
					{
						scriptBuffer.Append( clientCode );
					}
				}
				//尽管是RunClientCodeOnly，仍需要重置InfoJet_Postbacking和隐藏Progress。
				scriptBuffer.Append( "parent.InfoJet_StopProgress();" );
				scriptBuffer.Append( "parent.InfoJet_Postbacking = 0;" );
				//CID 0267 //2009.04.07 //Begin
				scriptBuffer.Append( "parent.InfoJet_OnPostbackEnd();" );
				//CID 0267 //2009.04.07 //End
				scriptBuffer.Append( "</script>" );
				return scriptBuffer.ToString();
			}

			NoRefreshViewNode noRefreshViewNode = null;
			if( form.ValueChangeNode != null )
			{
				noRefreshViewNode = form.Manifest.FormConfiguration.findNoRefreshViewNode( form.ValueChangeNode );
			}

			StringBuilder htmlBuffer = new StringBuilder();
			htmlBuffer.Append("<html>");
			htmlBuffer.Append("<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></meta></head>");
			htmlBuffer.Append("<body>");

			XmlElement viewElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.ViewId + "']" );
			XmlElement formElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.FormId + "']" );
			XmlElement dataElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/div[@id='" + JetConstant.DataId + "']" );
			//找到Script
			XmlElement scriptElement = (XmlElement)form.HtmlDoc.DocumentElement.SelectSingleNode( "/div/script[@id='" + JetConstant.ScriptId + "']" );

			if( noRefreshViewNode != null )
			{
				htmlBuffer.Append( "<input type='hidden' id='" + JetConstant.ViewId + "' value=\"\"/>" );
			}
			else
			{
				htmlBuffer.Append( "<input type='hidden' id='" + JetConstant.ViewId + "' value=\"" + System.Web.HttpUtility.HtmlEncode( viewElement.InnerXml ) + "\"/>" );
			}
			htmlBuffer.Append( "<input type='hidden' id='" + JetConstant.FormId + "' value=\"" + System.Web.HttpUtility.HtmlEncode( formElement.InnerXml ) + "\"/>" );
			htmlBuffer.Append( "<input type='hidden' id='" + JetConstant.DataId + "' value=\"" + System.Web.HttpUtility.HtmlEncode( dataElement.InnerXml ) + "\"/>" );
			
			htmlBuffer.Append( "<script>" );
			htmlBuffer.Append("parent.InfoJet_ReloadForm();");

			//CID 0296 //2009.06.23 //Begin
			if( form.NewStructureNode != null )
			{
				string newStrctureNodeId = DOMUtil.getNodeJetId( form.NewStructureNode );
				htmlBuffer.Append( "parent.InfoJet_NewStructureNodeId = '" + newStrctureNodeId + "';" );
			}
			//CID 0296 //2009.06.23 //End

			//重置InfoJet_Postbacking和隐藏Progress。
			htmlBuffer.Append( "parent.InfoJet_StopProgress();" );
			htmlBuffer.Append( "parent.InfoJet_Postbacking = 0;" );
			//CID 0087 //2008.08.05 //Begin
			htmlBuffer.Append( "parent.InfoJet_OnPostbackEnd();" );
			//CID 0087 //2008.08.05 //End
			
			//CID 0255 //2009.05.21 //Begin
			//CID 0400 //2009.12.02 //Begin
			if( form.IsOnOpenView )
			{
				htmlBuffer.Append( "parent.InfoJet_FocusControlId = null;" );
			}
			else
			{
				htmlBuffer.Append( "parent.InfoJet_RestoreFocus();" );
			}
			//CID 0400 //2009.12.02 //End
			//CID 0255 //2009.05.21 //End

			htmlBuffer.Append( "</script>" );

			//将Script直接输出到IFrame中执行.
			htmlBuffer.Append( DOMUtil.toString( scriptElement ) );

			//CID 0474 //2009.12.23 //Begin
			htmlBuffer.Append( "parent.InfoJet_ClearCachedPostback();" );
			//CID 0474 //2009.12.23 //End

			htmlBuffer.Append("</body></html>");

			return htmlBuffer.ToString();
		}

	}
}